package com.example.administrator.uhf;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.uhf.reader.helper.ReaderHelper;
import com.nativec.tools.ModuleManager;
import com.nativec.tools.SerialPort;
import com.nativec.tools.SerialPortFinder;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

public class Start extends BaseActivity {
    private List<String> mPortList = new ArrayList<String>();
    public static SerialPort mSerialPort = null;
    String[] entryValues = null;
    private int mPosPort ;
    Button btn;
    private SerialPortFinder mSerialPortFinder;
    private static final String TTYS1 = "ttyS4 (rk_serial)";  //串口号
    private static final String TTYS2 = "/dev/ttyS4";
    private int baud = 115200;  //波特率
    private ReaderHelper mReaderHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        ((UHFApplication)getApplication()).addActivity(this);
       // mSerialPortFinder = new SerialPortFinder();
      //  entryValues = mSerialPortFinder.getAllDevicesPath();
      btn=findViewById(R.id.btn);
      btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              StartConnect();
          }
      });
    }


    public void StartConnect(){



        try {
            mSerialPort = new SerialPort(new File(TTYS2), baud, 0);


            try {

                mReaderHelper = ReaderHelper.getDefaultHelper();
                mReaderHelper.setReader(mSerialPort.getInputStream(), mSerialPort.getOutputStream());


            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
            if (!ModuleManager.newInstance().setUHFStatus(true)) {
                 throw new RuntimeException("UHF RFID power on failure,may you open in other" +
                        " Process and do not exit it");
            }
            Intent intent;
           intent=new Intent(Start.this,WriteTag.class);
            startActivity(intent);


        }catch (SecurityException e) {
                Toast.makeText(
                        getApplicationContext(),
                        getResources().getString(R.string.error_security),
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(
                        getApplicationContext(),
                        getResources().getString(R.string.error_unknown),
                        Toast.LENGTH_SHORT).show();
            } catch (InvalidParameterException e) {
                Toast.makeText(
                        getApplicationContext(),
                        getResources().getString(R.string.error_configuration),
                        Toast.LENGTH_SHORT).show();
            }


    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}



